package com.drojian.demo3.dataStorage.mmkv;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.drojian.demo.R;
import com.tencent.mmkv.MMKV;

public class MMKVActivity extends AppCompatActivity {
    private EditText name_text;
    private EditText name_age;
    private EditText name_married;
    private MMKV rootDir;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.part3_activity_mmkv_activity);

        Button button = findViewById(R.id.mmkv_btn);
        Button button2 = findViewById(R.id.mmkv_btn2);
        name_text = findViewById(R.id.mmkv_edit1);
        name_age = findViewById(R.id.mmkv_edit2);
        name_married = findViewById(R.id.mmkv_edit3);

        MMKV.initialize(this);
        rootDir = MMKV.defaultMMKV();
        button.setOnClickListener(v->{
            rootDir.encode("name","许家其");rootDir.encode("age",22);rootDir.encode("married",false);
            // rootDir.clear();
            // rootDir.encode("name","张三");rootDir.encode("age",22);rootDir.encode("married",true);

        });
        button2.setOnClickListener(v->{
            String name = rootDir.getString("name", null);
            int age = rootDir.getInt("age",0);
            boolean married = rootDir.getBoolean("married",false);
            name_text.setText("姓名："+name);
            name_age.setText("年龄为："+age);
            name_married.setText("婚姻状况为："+married);
        });


    }
}